<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pgmpy.factors.continuous.discretize &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../../index.html">
              <img class="logo" src="../../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for pgmpy.factors.continuous.discretize</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">division</span>

<span class="kn">from</span> <span class="nn">six</span> <span class="k">import</span> <span class="n">with_metaclass</span>
<span class="kn">from</span> <span class="nn">abc</span> <span class="k">import</span> <span class="n">ABCMeta</span><span class="p">,</span> <span class="n">abstractmethod</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">scipy</span> <span class="k">import</span> <span class="n">integrate</span>


<div class="viewcode-block" id="BaseDiscretizer"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.discretize.BaseDiscretizer">[docs]</a><span class="k">class</span> <span class="nc">BaseDiscretizer</span><span class="p">(</span><span class="n">with_metaclass</span><span class="p">(</span><span class="n">ABCMeta</span><span class="p">)):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base class for the discretizer classes in pgmpy. The discretizer</span>
<span class="sd">    classes are used to discretize a continuous random variable</span>
<span class="sd">    distribution into discrete probability masses.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    factor: A ContinuousNode or a ContinuousFactor object</span>
<span class="sd">        the continuous node or factor representing the distribution</span>
<span class="sd">        to be discretized.</span>

<span class="sd">    low, high: float</span>
<span class="sd">        the range over which the function will be discretized.</span>

<span class="sd">    cardinality: int</span>
<span class="sd">        the number of states required in the discretized output.</span>

<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    &gt;&gt;&gt; from scipy.stats import norm</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousNode</span>
<span class="sd">    &gt;&gt;&gt; normal = ContinuousNode(norm(0, 1).pdf)</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.discretize import BaseDiscretizer</span>
<span class="sd">    &gt;&gt;&gt; class ChildDiscretizer(BaseDiscretizer):</span>
<span class="sd">    ...     def get_discrete_values(self):</span>
<span class="sd">    ...         pass</span>
<span class="sd">    &gt;&gt;&gt; discretizer = ChildDiscretizer(normal, -3, 3, 10)</span>
<span class="sd">    &gt;&gt;&gt; discretizer.factor</span>
<span class="sd">    &lt;pgmpy.factors.continuous.ContinuousNode.ContinuousNode object at 0x04C98190&gt;</span>
<span class="sd">    &gt;&gt;&gt; discretizer.cardinality</span>
<span class="sd">    10</span>
<span class="sd">    &gt;&gt;&gt; discretizer.get_labels()</span>
<span class="sd">    [&#39;x=-3.0&#39;, &#39;x=-2.4&#39;, &#39;x=-1.8&#39;, &#39;x=-1.2&#39;, &#39;x=-0.6&#39;, &#39;x=0.0&#39;, &#39;x=0.6&#39;, &#39;x=1.2&#39;, &#39;x=1.8&#39;, &#39;x=2.4&#39;]</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">factor</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">cardinality</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">factor</span> <span class="o">=</span> <span class="n">factor</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="n">low</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">high</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">=</span> <span class="n">cardinality</span>

    <span class="nd">@abstractmethod</span>
<div class="viewcode-block" id="BaseDiscretizer.get_discrete_values"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.discretize.BaseDiscretizer.get_discrete_values">[docs]</a>    <span class="k">def</span> <span class="nf">get_discrete_values</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method implements the algorithm to discretize the given</span>
<span class="sd">        continuous distribution.</span>

<span class="sd">        It must be implemented by all the subclasses of BaseDiscretizer.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        A list of discrete values or a DiscreteFactor object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="BaseDiscretizer.get_labels"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.discretize.BaseDiscretizer.get_labels">[docs]</a>    <span class="k">def</span> <span class="nf">get_labels</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a list of strings representing the values about</span>
<span class="sd">        which the discretization method calculates the probabilty</span>
<span class="sd">        masses.</span>

<span class="sd">        Default value is the points -</span>
<span class="sd">        [low, low+step, low+2*step, ......... , high-step]</span>
<span class="sd">        unless the method is overridden by a subclass.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors import ContinuousNode</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.discretize import BaseDiscretizer</span>
<span class="sd">        &gt;&gt;&gt; class ChildDiscretizer(BaseDiscretizer):</span>
<span class="sd">        ...     def get_discrete_values(self):</span>
<span class="sd">        ...         pass</span>
<span class="sd">        &gt;&gt;&gt; from scipy.stats import norm</span>
<span class="sd">        &gt;&gt;&gt; node = ContinuousNode(norm(0).pdf)</span>
<span class="sd">        &gt;&gt;&gt; child = ChildDiscretizer(node, -5, 5, 20)</span>
<span class="sd">        &gt;&gt;&gt; chld.get_labels()</span>
<span class="sd">        [&#39;x=-5.0&#39;, &#39;x=-4.5&#39;, &#39;x=-4.0&#39;, &#39;x=-3.5&#39;, &#39;x=-3.0&#39;, &#39;x=-2.5&#39;,</span>
<span class="sd">         &#39;x=-2.0&#39;, &#39;x=-1.5&#39;, &#39;x=-1.0&#39;, &#39;x=-0.5&#39;, &#39;x=0.0&#39;, &#39;x=0.5&#39;, &#39;x=1.0&#39;,</span>
<span class="sd">         &#39;x=1.5&#39;, &#39;x=2.0&#39;, &#39;x=2.5&#39;, &#39;x=3.0&#39;, &#39;x=3.5&#39;, &#39;x=4.0&#39;, &#39;x=4.5&#39;]</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">step</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span>
        <span class="n">labels</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;x=</span><span class="si">{i}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span>
            <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">low</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span><span class="p">,</span> <span class="n">step</span><span class="p">),</span> <span class="mi">3</span><span class="p">)]</span>
        <span class="k">return</span> <span class="n">labels</span></div></div>


<div class="viewcode-block" id="RoundingDiscretizer"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.discretize.RoundingDiscretizer">[docs]</a><span class="k">class</span> <span class="nc">RoundingDiscretizer</span><span class="p">(</span><span class="n">BaseDiscretizer</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class uses the rounding method for discretizing the</span>
<span class="sd">    given continuous distribution.</span>

<span class="sd">    For the rounding method,</span>

<span class="sd">    The probability mass is,</span>
<span class="sd">    cdf(x+step/2)-cdf(x), for x = low</span>

<span class="sd">    cdf(x+step/2)-cdf(x-step/2), for low &lt; x &lt;= high</span>

<span class="sd">    where, cdf is the cumulative density function of the distribution</span>
<span class="sd">    and step = (high-low)/cardinality.</span>

<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    &gt;&gt;&gt; import numpy as np</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.factors.continuous import ContinuousNode</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.factors.continuous import RoundingDiscretizer</span>
<span class="sd">    &gt;&gt;&gt; std_normal_pdf = lambda x : np.exp(-x*x/2) / (np.sqrt(2*np.pi))</span>
<span class="sd">    &gt;&gt;&gt; std_normal = ContinuousNode(std_normal_pdf)</span>
<span class="sd">    &gt;&gt;&gt; std_normal.discretize(RoundingDiscretizer, low=-3, high=3,</span>
<span class="sd">    ...                       cardinality=12)</span>
<span class="sd">    [0.001629865203424451, 0.009244709419989363, 0.027834684208773178,</span>
<span class="sd">     0.065590616803038182, 0.120977578710013, 0.17466632194020804,</span>
<span class="sd">     0.19741265136584729, 0.17466632194020937, 0.12097757871001302,</span>
<span class="sd">     0.065590616803036905, 0.027834684208772664, 0.0092447094199902269]</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">get_discrete_values</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">step</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span>

        <span class="c1"># for x=[low]</span>
        <span class="n">discrete_values</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">factor</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">+</span> <span class="n">step</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">factor</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">low</span><span class="p">)]</span>

        <span class="c1"># for x=[low+step, low+2*step, ........., high-step]</span>
        <span class="n">points</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">+</span> <span class="n">step</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="n">step</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">discrete_values</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">factor</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="n">step</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">factor</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">i</span> <span class="o">-</span> <span class="n">step</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">points</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">discrete_values</span></div>


<div class="viewcode-block" id="UnbiasedDiscretizer"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.discretize.UnbiasedDiscretizer">[docs]</a><span class="k">class</span> <span class="nc">UnbiasedDiscretizer</span><span class="p">(</span><span class="n">BaseDiscretizer</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class uses the unbiased method for discretizing the</span>
<span class="sd">    given continuous distribution.</span>

<span class="sd">    The unbiased method for discretization is the matching of the</span>
<span class="sd">    first moment method. It involves calculating the first order</span>
<span class="sd">    limited moment of the distribution which is done by the _lim_moment</span>
<span class="sd">    method.</span>

<span class="sd">    For this method,</span>

<span class="sd">    The probability mass is,</span>
<span class="sd">    (E(x) - E(x + step))/step + 1 - cdf(x), for x = low</span>

<span class="sd">    (2 * E(x) - E(x - step) - E(x + step))/step, for low &lt; x &lt; high</span>

<span class="sd">    (E(x) - E(x - step))/step - 1 + cdf(x), for x = high</span>

<span class="sd">    where, E(x) is the first limiting moment of the distribution</span>
<span class="sd">    about the point x, cdf is the cumulative density function</span>
<span class="sd">    and step = (high-low)/cardinality.</span>

<span class="sd">    Reference</span>
<span class="sd">    ---------</span>
<span class="sd">    Klugman, S. A., Panjer, H. H. and Willmot, G. E.,</span>
<span class="sd">    Loss Models, From Data to Decisions, Fourth Edition,</span>
<span class="sd">    Wiley, section 9.6.5.2 (Method of local monment matching) and</span>
<span class="sd">    exercise 9.41.</span>

<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    &gt;&gt;&gt; import numpy as np</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.factors import ContinuousNode</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.factors.continuous import UnbiasedDiscretizer</span>
<span class="sd">    # exponential distribution with rate = 2</span>
<span class="sd">    &gt;&gt;&gt; exp_pdf = lambda x: 2*np.exp(-2*x) if x&gt;=0 else 0</span>
<span class="sd">    &gt;&gt;&gt; exp_node = ContinuousNode(exp_pdf)</span>
<span class="sd">    &gt;&gt;&gt; exp_node.discretize(UnbiasedDiscretizer, low=0, high=5, cardinality=10)</span>
<span class="sd">    [0.39627368905806137, 0.4049838434034298, 0.13331784003148325,</span>
<span class="sd">     0.043887287876647259, 0.014447413395300212, 0.0047559685431339703,</span>
<span class="sd">     0.0015656350182896128, 0.00051540201980112557, 0.00016965346326140994,</span>
<span class="sd">     3.7867260839208328e-05]</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">get_discrete_values</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">lev</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lim_moment</span>
        <span class="n">step</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>

        <span class="c1"># for x=[low]</span>
        <span class="n">discrete_values</span> <span class="o">=</span> <span class="p">[(</span><span class="n">lev</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">-</span> <span class="n">lev</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">+</span> <span class="n">step</span><span class="p">))</span> <span class="o">/</span> <span class="n">step</span> <span class="o">+</span>
                           <span class="mi">1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">factor</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">low</span><span class="p">)]</span>

        <span class="c1"># for x=[low+step, low+2*step, ........., high-step]</span>
        <span class="n">points</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">low</span> <span class="o">+</span> <span class="n">step</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="n">step</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span>
        <span class="n">discrete_values</span><span class="o">.</span><span class="n">extend</span><span class="p">([(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">lev</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="o">-</span> <span class="n">lev</span><span class="p">(</span><span class="n">i</span> <span class="o">-</span> <span class="n">step</span><span class="p">)</span> <span class="o">-</span> <span class="n">lev</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="n">step</span><span class="p">))</span> <span class="o">/</span> <span class="n">step</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">points</span><span class="p">])</span>

        <span class="c1"># for x=[high]</span>
        <span class="n">discrete_values</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">lev</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">high</span><span class="p">)</span> <span class="o">-</span> <span class="n">lev</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="n">step</span><span class="p">))</span> <span class="o">/</span> <span class="n">step</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">factor</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">high</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">discrete_values</span>

    <span class="k">def</span> <span class="nf">_lim_moment</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method calculates the kth order limiting moment of</span>
<span class="sd">        the distribution. It is given by -</span>

<span class="sd">        E(u) = Integral (-inf to u) [ (x^k)*pdf(x) dx ] + (u^k)(1-cdf(u))</span>

<span class="sd">        where, pdf is the probability density function and cdf is the</span>
<span class="sd">        cumulative density function of the distribution.</span>

<span class="sd">        Reference</span>
<span class="sd">        ---------</span>
<span class="sd">        Klugman, S. A., Panjer, H. H. and Willmot, G. E.,</span>
<span class="sd">        Loss Models, From Data to Decisions, Fourth Edition,</span>
<span class="sd">        Wiley, definition 3.5 and equation 3.8.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        u: float</span>
<span class="sd">            The point at which the moment is to be calculated.</span>

<span class="sd">        order: int</span>
<span class="sd">            The order of the moment, default is first order.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">def</span> <span class="nf">fun</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">power</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">order</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">factor</span><span class="o">.</span><span class="n">pdf</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">integrate</span><span class="o">.</span><span class="n">quad</span><span class="p">(</span><span class="n">fun</span><span class="p">,</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="n">u</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span>
                <span class="n">np</span><span class="o">.</span><span class="n">power</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">order</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">factor</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="n">u</span><span class="p">)))</span>

    <span class="k">def</span> <span class="nf">get_labels</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">labels</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="s1">&#39;x=</span><span class="si">{i}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">round</span>
                      <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">low</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">high</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">),</span> <span class="mi">3</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">labels</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>